package com.jiangruan.javaproject.request;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;
import java.util.Set;

/**
 * @author:张子寒
 * @email:1594426628@qq.com
 * @desc:该函数用于实现get请求，并将请求结果进行返回
 * @DateTime:2021-12-04
 **/
public class HttpUtils {
    public static String get(String path){
        String line="";//最开始的空行
        String result="";//每次读取后加入result
        BufferedReader reader=null;
        InputStreamReader inputStreamReader=null;
        InputStream inputStream=null;
        try {
            //Union Resource Location 统一资源定位
            URL url=new URL(path);
            URLConnection conn = url.openConnection();
            //进行连接
            conn.connect();
            inputStream = conn.getInputStream();
            //字符流：按行读
             inputStreamReader=new InputStreamReader(inputStream);
             reader=new BufferedReader(inputStreamReader);
            while ((line=reader.readLine())!=null){
                result+=line;
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //关闭读取流
            try {
                if (reader != null) {
                    reader.close();
                }
                if (inputStreamReader !=null){
                    inputStreamReader.close();
                }
                if (inputStream!=null){
                    inputStream.close();
                }
            }catch (IOException e) {
                    e.printStackTrace();
            }
        }
        return result;
    }
    public static String post(String path, Map<String,String> header,byte[] data){
        OutputStream outputStream=null;
        InputStream inputStream=null;
        BufferedReader reader=null;
        String result="";
        try {
            URL url=new URL(path);
            HttpURLConnection conn= (HttpURLConnection) url.openConnection();
            //设置可以发送数据和接收数据
            conn.setDoInput(true);
            conn.setDoOutput(true);
            //请求头信息设置
            //ContentType:opplication/json
            //Authorization:"Basic"+base64(xxx)
            //如何将map中的数据读取出来
            //集合:本质上是容器
            //List:有序，数据可以重复
            //Set:无序，数据不能重复
            //Map:无序，key不能重复
            if (!header.isEmpty()){
            Set<String> keys = header.keySet();
            for (String key:keys){
                String value=header.get(key);
                conn.setRequestProperty(key,value);
                }
            }
            conn.setRequestMethod("POST");
            conn.connect();
            //将请求的数据发送出去
            outputStream= conn.getOutputStream();
            outputStream.write(data);
            //接受返回的响应数据
            inputStream= conn.getInputStream();
            //io流
            //输入流：字节输入流（InputStream） 字符读取流（Reader）
            //输出流：字节输出流（outputStream）字符写入流（Writer）
            //转换流：字节输入读取流（InputStreamReader） 字节输出写入流（outputStreamWriter）
            reader= new BufferedReader(new InputStreamReader(inputStream));
            String Line=null;
            while ((Line=reader.readLine())!=null){
                result+=Line;
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (reader != null) {
                    reader.close();
                }
                if (inputStream != null) {
                    inputStream.close();
                }
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }
}